package cn.bon.emp.controller;

import cn.bon.common.bean.JsonResult;
import cn.bon.common.exception.BizIllegalException;
import cn.bon.common.tool.R;
import cn.bon.emp.api.pojo.dto.DetailDTO;
import cn.bon.emp.api.pojo.dto.EmpsDTO;
import cn.bon.emp.api.pojo.po.Org;
import cn.bon.emp.api.pojo.vo.DetailVO;
import cn.bon.emp.api.pojo.vo.EmpInfoVO;
import cn.bon.emp.api.pojo.vo.NotGroupedVO;
import cn.bon.emp.service.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author Lc_cl
 * @Date 2023/12/9
 * @Description
 */
@Tag(name = "员工相关接口")
@RestController
@RequestMapping("/emp")
public class EmpController {
    @Resource
    IEmpInfoService empInfoService;
    @Resource
    IEmpDetailsService empDetailsService;
    @Resource
    IEmpOrgService empOrgService;
    @Resource
    IOrgService orgService;

    @Operation(summary = "分页查询员工")
    @PostMapping("/page/{id}")
    public JsonResult<Page<EmpInfoVO>> page(@RequestBody(required = false) EmpsDTO dto,
                                            @PathVariable("id")@Schema(description = "所属机构Id") Long orgId) {
        Page<EmpInfoVO> page = empInfoService.doPage(dto, orgId);

        return R.success(page);
    }

    @Operation(summary = "批量备勤")
    @PutMapping("/duty/{status}")
    public JsonResult<String> changeDuty(@PathVariable("status")@Schema(description = "更改状态") Integer status,
                                         @RequestBody() Long[] empId){

        empInfoService.setStatus(empId, status);

        return R.success("修改成功");
    }

    @Operation(summary = "查询单个员工详情")
    @GetMapping("/{id}")
    public JsonResult<DetailVO> details(@PathVariable("id") @Schema(description = "员工id") Long id) {
        DetailVO vo = empDetailsService.getDetails(id);
        return R.success(vo);
    }

    @Operation(summary = "修改单个员工详情")
    @PutMapping("/{id}")
    public JsonResult<String> updateEmp(@PathVariable("id") @Schema(description = "员工id") Long id,
                                        @RequestBody DetailDTO dto) {
        empDetailsService.updateEmp(id,dto);
        return R.success("修改成功");
    }


    @Operation(summary = "移除员工所在的小组")
    @PostMapping("/rmGroup")
    public JsonResult<String> deleteFromGroup(@RequestBody List<Long> empIds) {
        empOrgService.rmFromGroup(empIds);
        return R.success("移除小组成功");
    }

    @Operation(summary = "获取指定部门下未分组的所有员工")
    @GetMapping("/ofDept/{deptId}")
    public JsonResult<List<NotGroupedVO>> getNotGrouped(@PathVariable Long deptId) {
        List<NotGroupedVO> vos = empInfoService.getNotGrouped(deptId);
        return R.success(vos);
    }

    @Operation(summary = "批量添加员工到指定组")
    @PutMapping("/toGroup/{groupId}")
    public JsonResult<String> addEmp2Group(@PathVariable Long groupId,
                                           @RequestBody List<Long> empIds) {
        //校验 小组是否存在
        Org group = orgService.getById(groupId);
        if (group==null) throw new BizIllegalException("该小组不存在");
        //获取科室id
        Long sectionId = group.getParentId();

        empOrgService.addEmps2Group(groupId,sectionId, empIds);
        return R.success("添加成功");
    }

}
